package com.android.calldelay.alarmServices;

import java.util.Calendar;

import com.android.calldelay.AppInfo;
import com.android.calldelay.MainActivity;
import com.android.calldelay.OnAlarmReceive;
import com.android.calldelay.OnAlertReceave;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.Toast;

public class SetAlarmService {

	private Context context;

	public SetAlarmService(Context context) {
		this.context = context ;
	}




	public void setupAlarm(boolean isStart) {

		try {

			AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
			SharedPreferences sharedPreferences = context.getSharedPreferences(AppInfo.APP_PREF, 0);
			int totalDialTime = sharedPreferences.getInt(AppInfo.KEY_TOTAL_DIAL_TIME, 0);
			//boolean isStart = sharedPreferences.getBoolean(AppInfo.KEY_CALL_DELAY_APP_START_STOP, false);
			Intent intent = new Intent(context, OnAlarmReceive.class);
		 
			PendingIntent pendingIntent = PendingIntent.getBroadcast(
					context, 0, intent,
					PendingIntent.FLAG_UPDATE_CURRENT);

			// Getting current time and add the seconds in it
			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.SECOND, totalDialTime);
			if (AppInfo.FLAG_ALARM_START) {
				if (isStart) {
					alarmManager.set(AlarmManager.RTC_WAKEUP,
							cal.getTimeInMillis(), pendingIntent);
					AppInfo.FLAG_ALARM_START = false;
					showToast("Call delayed is applied...");

				} 
			}else {
				if (!isStart ) {
					alarmManager.cancel(pendingIntent);
					showToast("Call delay is successfully stopped.");
					AppInfo.FLAG_ALARM_START = true;
				}else {
					showToast("Call delay is already started.");		
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("" + e);
		}

	}




	private void showToast(String msg) {
		Toast.makeText(context,msg, Toast.LENGTH_SHORT).show();
		//getCallLog();
	}



	// setting up the alarm for showing the alert before dialing
	public void setupAlarm(int seconds) {


		AlarmManager alarmManager = (AlarmManager) context.getSystemService(context.ALARM_SERVICE);
		Intent intent = new Intent(context, OnAlertReceave.class);

		PendingIntent pendingIntent = PendingIntent
				.getBroadcast(context, 0, intent,
						PendingIntent.FLAG_UPDATE_CURRENT);

		// Getting current time and add the seconds in it
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.SECOND, seconds);
		alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
				pendingIntent);




	}
	// getting the string data from preferences
		private String getStringData(String key) {
			SharedPreferences setting = context.getSharedPreferences(AppInfo.APP_PREF,
					0);
			return setting.getString(key, "");
		}



}
